//
//  FestivalTableViewCell.m
//  代号：47
//
//  Created by 杨佩璋 on 15/6/30.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "FestivalTableViewCell.h"
#import <UIImageView+WebCache.h>

@implementation FestivalTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _picture = [[UIImageView alloc] init];
        _picture.frame = CGRectMake(10, 10, 100, 100);
        
        _titleLabel = [[UILabel alloc] init];
        CGFloat titleWidth = kScreen_width - 120 -10;
        _titleLabel.frame = CGRectMake(120, 5, titleWidth, 60);
        _titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _titleLabel.textColor = [UIColor orangeColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        
        _title2Label = [[UILabel alloc] init];
        _title2Label.frame = CGRectMake(120, 55, titleWidth, 20);
        _titleLabel.numberOfLines = 0;
        _title2Label.font = [UIFont systemFontOfSize:12];
        _title2Label.textColor = [UIColor lightGrayColor];
        _title2Label.textAlignment = NSTextAlignmentCenter;
        
        
        self.play = [[UIImageView alloc] init];
        [_play release];

        _commentLabel = [[UILabel alloc] init];
        _commentLabel.frame = CGRectMake(120, 90, titleWidth, 20);
        _commentLabel.font = [UIFont systemFontOfSize:10];
        _commentLabel.textColor = [UIColor lightGrayColor];
        _commentLabel.textAlignment = NSTextAlignmentRight;
        
        _publishTime = [[UILabel alloc] init];
        _publishTime.frame = CGRectMake(120, 90, titleWidth, 20);
        _publishTime.font = [UIFont systemFontOfSize:10];
        _publishTime.textColor = [UIColor lightGrayColor];
        _publishTime.textAlignment = NSTextAlignmentLeft;
        
        
        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_title2Label];
        [self.contentView addSubview:_picture];
        [self.contentView addSubview:_commentLabel];
        [self.contentView addSubview:_publishTime];
        
        
        [_picture release];
        [_title2Label release];
        [_titleLabel release];
        [_commentLabel release];
        [_publishTime release];
        
        
    }
    return  self;
}



- (void)setModel:(FestivalModel *)model
{
    
    _play.frame = CGRectZero;
    
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    [_picture sd_setImageWithURL:[NSURL URLWithString:model.image]];
        NSNumber *type = model.type;
    int num = [type intValue];
    if (num == 2) {
       _play.frame = CGRectMake((100-30.0)/2, (100-30.0)/2, 30, 30);
        _play.image = [UIImage imageNamed:@"iconfont-bofang(2)"];
        [_picture addSubview:_play];
    }
    
    _titleLabel.text = model.title;
    _title2Label.text = model.title2;
    
    NSString *count = [NSString stringWithFormat:@"%@",model.commentCount];
    _commentLabel.text = [NSString stringWithFormat:@"评论数%@",count];
    
    double time = [model.publishTime doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSDate *currentDate = [NSDate date];
    NSDate *newDate = [NSDate dateWithTimeInterval:8*60*60 sinceDate:currentDate];
    NSTimeInterval minus = [newDate timeIntervalSinceDate:date];
    /*分钟*/
    NSTimeInterval minute =  minus/60;
    /*小时*/
    NSTimeInterval hour = minute/60;
    NSString *timeStr;
    if (minute < 60) {
        timeStr = [NSString stringWithFormat:@"%.0f分钟前",minute];
    }else if (hour<24){
        timeStr = [NSString stringWithFormat:@"%.0f小时前",hour];
    }else{
        timeStr = [NSString stringByDate:date];
    }
    _publishTime.text = timeStr;
   
}



-(void)dealloc{
    
    [_play release];
    [_model release];
    [super dealloc];
}

@end
